package com.wuchao.service.serviceImpl;

import com.wuchao.basedao.BaseDao;
import com.wuchao.entity.User;
import com.wuchao.service.Service;
import com.wuchao.service.ServiceDatasource;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/11 12:02
 */
public class ServiceDatasourceImpl extends BaseDao implements ServiceDatasource {
    static Logger logger=Logger.getLogger(ServiceDatasourceImpl.class.getName());//日志
    @Override
    public List<User> findPoJo(String sql, Object... params) {
        List<User> list=new ArrayList<>();//必须完整
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        connection=getConnection1();
        try {
            preparedStatement=connection.prepareStatement(sql); //要挖坑填坑
            if(params!=null){
                for(int i=0;i<params.length;i++){//一定要从1开始
                    preparedStatement.setObject((i+1),params[i]);
                }
            }
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                User pojo = new User();
                pojo.setId(resultSet.getInt(1));
                pojo.setName(resultSet.getString(2));
                pojo.setSex(resultSet.getInt(3));
                pojo.setHobby(resultSet.getString(4));
                list.add(pojo);
            }//connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }



}
